<?php
defined('IN_PHPCMS') or exit('No permission resources.');
//模型缓存路径
define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);

pc_base::load_app_func('util','anjia');
pc_base::load_app_func('dboper','anjia');

class organization {
	private $db;
	private $catid;
	function __construct() {
		$this->db = pc_base::load_model('anjia_content_model');
		$this->catid=12;
	}

	//添加机构
	public function add(){
	    $organizationName=$_POST['organizationName'];
	    $parentOrganId=$_POST['parentOrganId']?$_POST['parentOrganId']:'0';
	    $comments=$_POST['comments'];
	    $md5code=$_POST['md5'];
	    $checkResult=md5check($organizationName.$parentOrganId,$md5code);
	    if($checkResult)
	    {  
	        $addArray=array("title"=>$organizationName,"belongCompanyID"=>$parentOrganId,"comments"=>$comments,"catid"=>12);
	        $id=add($this->catid,$addArray,$this->db);
	        $respjson;
	        if(is_numeric($id)){
	            $respjson='{
	            "errNo": 0,
	            "result":{
	            "organizationId":"'.$id.'"
	        }
	        }';
	        }
	        else{
	            $respjson='{
	            "errNo": 1,
	            "result":
	           '.$id.'
	        
	        }';
	        }
	        echo $respjson;
	    }
	    else{
	        $respjson='{
	            "errNo": 1,
	            "result":{
	            "error":"InternalError"
	        }
	        }';
	        echo $respjson;
	    }
	}
	//获取机构
	public function search(){
	    $organizationID=$_GET['organizationId'];
	    $parentOrganId=$_GET['parentOrganId'];
	    $md5code=$_GET['md5'];
	    $pn=$_GET['pn'];
	    $num=$_GET['num'];
	    $checkResult=md5check($organizationID.$parentOrganId.$pn.$num,$md5code);
	    //组装查询条件
	    $where;  
	    if(!empty($organizationID)){
	       $where['id']=$organizationID;
	   }
	   if(!empty($parentOrganId)){
	       $where['belongCompanyID']=$parentOrganId;
	   }
	 //  $where['catid']=$this->catid;
	   $returndata='id,title,belongCompanyID';
	    if($checkResult)
	    {
	        $resultarray=lists($this->catid,$where,$pn,$num,$this->db,$returndata);
	        $respjson;
	    
	            $respjson='{
	            "errNo": 0,
	            "result":'.$resultarray.'
	        }';
	        
	      
	        
	        echo $respjson;
	
	    }
	    else{
	        $respjson='{
	            "errNo": 1,
	            "result":{
	            "error":"InternalError"
	        }
	        }';
	        echo $respjson;
	    }
	}
	//修改机构
	public function update(){
	    $organizationId=$_POST['organizationId'];
	    $organizationName=$_POST['organizationName'];
	    $parentOrganId=$_POST['parentOrganId'];
	    $comments=$_POST['comments'];
	    $md5code=$_POST['md5'];
	    $checkResult=md5check($organizationId.$organizationName.$parentOrganId.$comments,$md5code);
	    if($checkResult)
	    {
	        $updateArray=array("title"=>$organizationName,"belongCompanyID"=>$parentOrganId,"comments"=>$comments);
	        $result=update($this->catid,$organizationId,$updateArray,$this->db);
	        $respjson;
	        if($result)
	            $respjson='{
	            "errNo": 0,
	            "result":"更新成功"
	        }';
	        else{
	            $respjson='{
	            "errNo": 0,
	            "result":"更新失败"
	        }';
	        }
	        echo $respjson;
	    }
	    else{
	        $respjson='{
	            "errNo": 1,
	            "result":{
	            "error":"InternalError"
	        }
	        }';
	        echo $respjson;
	    }
	    
	}
	
	//删除机构
	public function delete(){
	    $organizationId=$_GET['organizationId'];
	    $md5code=$_GET['md5'];
	    $checkResult=md5check($organizationId,$md5code);
	    if($checkResult)
	    {
	        $result=delete($this->catid,$organizationId,$this->db);
	        $respjson;
	        if($result)
	            $respjson='{
	            "errNo": 0,
	            "result":"删除成功"
	        }';
	            else{
	                $respjson='{
	            "errNo": 0,
	            "result":"删除失败"
	        }';
	            }
	            echo $respjson;
	    }
	    else{
	        $respjson='{
	            "errNo": 1,
	            "result":{
	            "error":"internalError"
	        }
	        }';
	        echo $respjson;
	    }
	    
	}
	
	
}
?>